<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use think\DB;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Hardware extends Backend
{

  /*门锁配置*/
  public function index()
  {
      $hardware=DB::name('hardware')->order('time desc')->paginate(10);
      if($hardware->toArray())
      {
          foreach($hardware as $k=>$v)
          {
              $data=$v;
              if($v['state']==1)
              {
                 $data['state_name']='启用';
              }else{
                  $data['state_name']='禁用';
              }
              $hardware->offsetSet($k,$data);
          }
      }
      $this->assign('hardware',$hardware);
      $this->assign('page',$hardware->render());
      return $this->fetch('hardware/index');
  }
  /*
   * 添加门锁
   */
  public function add()
  {
      if($_POST)
      {
          $array=$this->request->param();
          $array['time']=time();
          $state=DB::name('hardware')->insert($array);
          if($state)
          {
            return  $this->success('添加成功',url('hardware/index'));
          }else{
           return   $this->error('添加失败');
          }
      }else{
          return $this->fetch('hardware/add');
      }
  }
  /*
   *修改门锁
   * */
  public function detail()
  {
      if($_POST)
      {
          $where['id']=$_POST['id'];
          $date['name']=$_POST['name'];
          $date['type']=$_POST['type'];
          $date['state']=$_POST['state'];
          $date['appid']=$_POST['appid'];
          $date['appsecret']=$_POST['appsecret'];
          $date['time']=time();
          $level=DB::name('hardware')->where($where)->update($date);
          return $this->success($level,'hardware/index');
      }else{
          $param = $this->request->param();
          $id=$param['id'];
          $hardware=DB::name('hardware')->where(array('id'=>$id))->find();
          $this->assign('hardware',$hardware);
          return $this->fetch('detail');
      }
  }
  /*删除*/
  public function delete()
  {
      $id=$this->request->param('id');
      $where['id']=$id;
      $state=DB::name('hardware')->where($where)->delete();
      if($state)
      {
           return $this->success('删除成功');

      }else{
          return $this->error('删除失败');
      }
  }
}
